//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hustcad.plm.pdm.file.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.caddocapi.service.TyplmCADDocCustomService;
import com.hustcad.plm.caddocapi.service.TyplmCADDocService;
import com.hustcad.plm.caddocapi.service.TyplmCADDocumentMasterService;
import com.hustcad.plm.caddocapi.service.TyplmCADDocumentService;
import com.hustcad.plm.caddocapi.service.TyplmCADReferenceLinkService;
import com.hustcad.plm.caddocapi.service.TyplmCADRelObjectService;
import com.hustcad.plm.caddocapi.service.cadstruct.TyplmCadStructQueryService;
import com.hustcad.plm.pdm.access.model.dto.AccessBatchDTO;
import com.hustcad.plm.pdm.access.service.TyplmAccessJudgeService;
import com.hustcad.plm.pdm.caddoc.model.dto.CadReferenceDTO;
import com.hustcad.plm.pdm.caddoc.model.dto.cadstruct.CadStructQueryDTO;
import com.hustcad.plm.pdm.caddoc.model.query.CadDocTreeQuery;
import com.hustcad.plm.pdm.caddoc.model.vo.CadDocTreeVO;
import com.hustcad.plm.pdm.caddoc.model.vo.cadstruct.CadStructBaseVO;
import com.hustcad.plm.pdm.common.component.CompletableFutureWrapper;
import com.hustcad.plm.pdm.common.util.CommonUtil;
import com.hustcad.plm.pdm.doc.service.TyplmDocQueryService;
import com.hustcad.plm.pdm.doc.service.TyplmDocumentService;
import com.hustcad.plm.pdm.doc.vo.DocDetails;
import com.hustcad.plm.pdm.file.model.constant.FileDownConstant;
import com.hustcad.plm.pdm.file.model.constant.FileErrorCodeEnum;
import com.hustcad.plm.pdm.file.model.entity.FileDownCenterDO;
import com.hustcad.plm.pdm.file.model.entity.FileDownQueueDO;
import com.hustcad.plm.pdm.file.model.vo.DownloadDocVO;
import com.hustcad.plm.pdm.file.model.vo.DownloadFileVO;
import com.hustcad.plm.pdm.file.model.vo.DownloadOptionVO;
import com.hustcad.plm.pdm.file.service.QueryDownloadDocService;
import com.hustcad.plm.pdm.file.util.FileUtil;
import com.hustcad.plm.pdm.file.util.QueryDownloadDocUtil;
import com.hustcad.plm.pdm.filevault.model.vo.FileDataVO;
import com.hustcad.plm.pdm.folder.service.TyplmSubFolderLinkService;
import com.hustcad.plm.pdm.middle.caddoc.model.enums.CadStructScopeEnum;
import com.hustcad.plm.pdm.preference.util.TyplmPreferenceValueUtil;
import com.ty.basic.entity.CTyEntityBaseDO;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.TyBaseObject;
import com.ty.basic.entity.cad.CADDocDO;
import com.ty.basic.entity.cad.CADDocMasterDO;
import com.ty.basic.entity.doc.DocDO;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.session.RequestContext;
import com.ty.basic.session.TyAccountContext;
import com.ty.basic.vo.PartRefDocVO;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class QueryDownloadDocServiceImpl implements QueryDownloadDocService {
    private static final Logger log = LoggerFactory.getLogger(QueryDownloadDocServiceImpl.class);
    @Resource
    private TyplmCADDocService typlmCADDocService;
    @Resource
    private TyplmCADDocCustomService typlmCADDocCustomService;
    @Resource
    private TyplmDocQueryService typlmDocQueryService;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;
    @Resource
    private TyplmSubFolderLinkService typlmSubFolderLinkService;
    @Resource
    private TyplmAccessJudgeService typlmAccessJudgeService;
    @Resource
    private TyplmPreferenceValueUtil typlmPreferenceValueUtil;
    @Resource
    private TyplmCADRelObjectService typlmCADRelObjectService;
    @Resource
    private TyplmCADDocumentService typlmCADDocumentService;
    @Resource
    private TyplmCADDocumentMasterService typlmCADDocumentMasterService;
    @Resource
    private TyplmCADReferenceLinkService typlmCADReferenceLinkService;
    @Resource
    private TyplmCadStructQueryService typlmCadStructQueryService;
    @Resource
    CompletableFutureWrapper completableFutureWrapper;

    public QueryDownloadDocServiceImpl() {
    }

    public void getDodAndCaddocListByDownloadDocVO(List<DownloadDocVO> selectObjList, List<DownloadDocVO> cadDocList, List<DownloadDocVO> docList, String isFolw, UserDO userDO) {
        log.info(">>>>getDodAndCaddocListByDownloadDocVO strat");
        if (!CollUtil.isEmpty(selectObjList)) {
            log.info(">>>selectDocList start");
            List<DownloadDocVO> selectDocList = (List)selectObjList.stream().filter((obj) -> {
                return "ty.inteplm.doc.CTyDoc".equals(obj.getOtype());
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(selectDocList)) {
                this.dealSelectDocList(selectDocList, docList, isFolw, userDO);
            }

            log.info(">>>selectCadDocList start");
            List<DownloadDocVO> selectCadDocList = (List)selectObjList.stream().filter((obj) -> {
                return "ty.inteplm.cad.CTyCADDoc".equals(obj.getOtype());
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(selectCadDocList)) {
                this.dealSelectCadDocList(cadDocList, isFolw, userDO, selectCadDocList);
            }

            log.info(">>>>getDodAndCaddocListByDownloadDocVO  end");
        }
    }

    private void dealSelectCadDocList(List<DownloadDocVO> cadDocList, String isFolw, UserDO userDO, List<DownloadDocVO> selectCadDocList) {
        List<DownloadDocVO> downloadCadDocVOList = new ArrayList();
        List<BigInteger> cadDocOidList = (List)selectCadDocList.stream().map((obj) -> {
            return CommonUtil.stringToBigInteger(obj.getOid());
        }).collect(Collectors.toList());
        List<CADDocDO> cadDocDOList = this.typlmCADDocService.queryCADDocByOIDs(cadDocOidList);
        List<CADDocDO> familyObjectByCadIdList = this.typlmCADDocumentService.queryFamilyObjectByCadIdList(cadDocOidList);
        List<CADDocMasterDO> familyCadDocMasterDOS;
        Map<BigInteger, List<CADDocDO>> cadidDoMap = (Map)familyObjectByCadIdList.stream().collect(Collectors.groupingBy(CADDocDO::getOid));
        List cadNumberList;
        if (CollUtil.isNotEmpty(familyObjectByCadIdList)) {
            cadNumberList = (List)familyObjectByCadIdList.stream().map((obj) -> {
                return obj.getMasteredoid();
            }).collect(Collectors.toList());
            familyCadDocMasterDOS = this.typlmCADDocumentMasterService.queryCADDocMasterByOIDs(cadNumberList);
            cadDocDOList.addAll(familyObjectByCadIdList);
        } else {
            familyCadDocMasterDOS = null;
        }

        if (CollUtil.isNotEmpty(cadDocDOList)) {
            downloadCadDocVOList = (List)cadDocDOList.stream().map(this::parseDownloadDocVOByCADDocDO).collect(Collectors.toList());
        }

        if (CollUtil.isNotEmpty((Collection)downloadCadDocVOList)) {
            if (CollUtil.isNotEmpty(familyCadDocMasterDOS)) {
                ((List<DownloadDocVO>)downloadCadDocVOList).forEach((caddocVo) -> {
                    if (StringUtils.isEmpty(caddocVo.getDocNumber())) {
                        List<CADDocDO> cadDocDOS = (List)cadidDoMap.get(new BigInteger(caddocVo.getOid()));
                        if (CollUtil.isNotEmpty(cadDocDOS)) {
                            List<CADDocMasterDO> docMasterDoList = (List)familyCadDocMasterDOS.stream().filter((docmaster) -> {
                                return docmaster.getOid().equals(((CADDocDO)cadDocDOS.get(0)).getMasteredoid());
                            }).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(docMasterDoList)) {
                                caddocVo.setDocNumber(((CADDocMasterDO)docMasterDoList.get(0)).getDocnumber());
                                caddocVo.setDocName(((CADDocMasterDO)docMasterDoList.get(0)).getName());
                            }
                        }
                    }

                });
            }

            cadNumberList = (List)((List<DownloadDocVO>)downloadCadDocVOList).stream().map(DownloadDocVO::getDocNumber).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(cadNumberList)) {
                log.info(">>>>cadNumberList:" + StringUtils.join(cadNumberList, ";"));
            }

            List<CTyEntityBaseDO> cTyEntityBaseDOList = new ArrayList(cadDocDOList);
            List<DownloadDocVO> downloadCadDocVOList1 = this.checkPermissions((List)downloadCadDocVOList, cTyEntityBaseDOList, userDO);
            cadDocList.addAll(downloadCadDocVOList1);
        }

    }

    private void dealSelectDocList(List<DownloadDocVO> selectDocList, List<DownloadDocVO> docList, String isFolw, UserDO userDO) {
        log.info(">>>>dealSelectDocList start");
        List<String> docOidList = (List)selectDocList.stream().map(DownloadDocVO::getOid).collect(Collectors.toList());
        Map<String, DocDO> docMap = new HashMap();
        List<DocDO> docDoList = new ArrayList();
        if (CollUtil.isNotEmpty(docOidList)) {
            log.info(">>>docOidList:" + StringUtils.join((Iterable)docDoList, ","));
            docDoList = this.typlmDocQueryService.getDocDOListByOids(docOidList);
        }

        if (CollUtil.isNotEmpty((Collection)docDoList)) {
            docMap = (Map)((List<DocDO>)docDoList).stream().collect(Collectors.toMap((obj) -> {
                return QueryDownloadDocUtil.parseStringByBigInteger(obj.getOid());
            }, Function.identity(), (k1, k2) -> {
                return k1;
            }));
        }

        Map<String, DocDO> finalDocMap = docMap;
        List<DownloadDocVO> docVOList = (List)selectDocList.stream().map((obj) -> {
            return this.parseDownloadDocVOByDocDO(obj, finalDocMap);
        }).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(docVOList)) {
            List<String> docNumberList = (List)docVOList.stream().map(DownloadDocVO::getDocNumber).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(docNumberList)) {
                log.info(">>>>docNumberList:" + StringUtils.join(docNumberList, ";"));
            }

            log.info(">>>> doc not in folw");
            List<CTyEntityBaseDO> cTyEntityBaseDOList = new ArrayList((Collection)docDoList);
            docVOList = this.checkPermissions(docVOList, cTyEntityBaseDOList, userDO);
            docList.addAll(docVOList);
        }

        log.info(">>>>dealSelectDocList start");
    }

    public DownloadDocVO parseDownloadDocVOByDocDO(DownloadDocVO downloadDocVO, Map<String, DocDO> docMap) {
        String docOid = downloadDocVO.getOid();
        DocDO docDO = (DocDO)docMap.get(docOid);
        if (ObjectUtils.isEmpty(docDO)) {
            return downloadDocVO;
        } else {
            downloadDocVO.setTypeOid(QueryDownloadDocUtil.parseStringByBigInteger(docDO.getTypeoid()));
            downloadDocVO.setDocSubfolderOid(QueryDownloadDocUtil.parseStringByBigInteger(docDO.getSubfolderoid()));
            downloadDocVO.setDocVersion(docDO.getVersion());
            downloadDocVO.setCompanyversion(docDO.getCompanyversion());
            downloadDocVO.setDocIterationid(docDO.getIterationid());
            downloadDocVO.setLockstateinfo(docDO.getLockstateinfo());
            downloadDocVO.setSourceiterationoid(QueryDownloadDocUtil.parseStringByBigInteger(docDO.getSourceiterationoid()));
            downloadDocVO.setSecuritylabels(docDO.getSecurities());
            QueryDownloadDocUtil.parseDefaultDownloadDocVOIsJudgeRight(downloadDocVO);
            return downloadDocVO;
        }
    }

    public DownloadDocVO parseDownloadDocVOByCADDocDO(CADDocDO cadDocDO) {
        if (ObjectUtils.isEmpty(cadDocDO)) {
            return null;
        } else {
            DownloadDocVO result = this.parseDefaultDownloadDocByCADDocDO(cadDocDO);
            if (ObjectUtils.isNotEmpty(result)) {
                QueryDownloadDocUtil.parseDefaultDownloadDocVOIsJudgeRight(result);
            }

            return result;
        }
    }

    public DownloadDocVO parseDownloadDocVOByRefCADDocDO(CADDocDO cadDocDO, String parentDownloadRight, String parentManageRight, String parentIsJudgeRight) {
        log.info(">>>parseDownloadDocVOByRefCADDocDO start");
        if (ObjectUtils.isEmpty(cadDocDO)) {
            return null;
        } else {
            String currentOid = QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getOid());
            log.info(">>>currentOid" + currentOid);
            DownloadDocVO result = this.parseDefaultDownloadDocByCADDocDO(cadDocDO);
            if (ObjectUtils.isEmpty(result)) {
                return null;
            } else {
                QueryDownloadDocUtil.parseDefaultDownloadDocVONoJudgeRight(result);
                this.checkRefObjPermission(result, parentDownloadRight, parentManageRight, parentIsJudgeRight);
                return result;
            }
        }
    }

    public DownloadDocVO parseDownloadDocVOByRefCADDocDO(CADDocDO cadDocDO, Map<String, List<String>> map, Map<String, DownloadDocVO> downloadDocVOMap) {
        log.info(">>>parseDownloadDocVOByRefCADDocDO start");
        if (!ObjectUtils.isEmpty(cadDocDO) && !CollUtil.isEmpty(map) && !CollUtil.isEmpty(downloadDocVOMap)) {
            String currentOid = QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getOid());
            log.info(">>>currentOid" + currentOid);
            if (downloadDocVOMap.containsKey(currentOid)) {
                return null;
            } else {
                DownloadDocVO result = this.parseDefaultDownloadDocByCADDocDO(cadDocDO);
                if (ObjectUtils.isEmpty(result)) {
                    return null;
                } else {
                    QueryDownloadDocUtil.parseDefaultDownloadDocVONoJudgeRight(result);
                    String currentMasterdoid = QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getMasteredoid());
                    log.info(">>>currentMasterdoid" + currentMasterdoid);
                    String parentOid = "";
                    if (map.containsKey(currentMasterdoid)) {
                        parentOid = (String)((List)map.get(currentMasterdoid)).get(0);
                    }

                    log.info(">>>parentOid" + parentOid);
                    if (StringUtils.isNoneBlank(new CharSequence[]{parentOid}) && downloadDocVOMap.containsKey(parentOid)) {
                        DownloadDocVO downloadDocVO = (DownloadDocVO)downloadDocVOMap.get(parentOid);
                        String parentDownloadRight = downloadDocVO.getHasDownloadRight();
                        String parentManageRight = downloadDocVO.getHasManageRight();
                        String isJudgeRight = downloadDocVO.getIsJudgeRight();
                        log.info(">>>parentDownloadRight" + parentDownloadRight + "parentManageRight" + parentManageRight + "isJudgeRight" + isJudgeRight);
                        this.checkRefObjPermission(result, parentDownloadRight, parentManageRight, isJudgeRight);
                    }

                    return result;
                }
            }
        } else {
            return null;
        }
    }

    public DownloadDocVO parseDefaultDownloadDocByCADDocDO(CADDocDO cadDocDO) {
        DownloadDocVO result = new DownloadDocVO();
        if (ObjectUtils.isEmpty(cadDocDO)) {
            return null;
        } else {
            result.setFamilytablestatus(cadDocDO.getFamilytablestatus());
            String docnumber = QueryDownloadDocUtil.parseStringByStr(cadDocDO.getDocnumber());
            result.setCombinName(docnumber);
            result.setTypeOid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getTypeoid()));
            result.setDocSubfolderOid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getSubfolderoid()));
            result.setOid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getOid()));
            result.setOtype(cadDocDO.getOtype());
            result.setDocVersion(cadDocDO.getVersion());
            result.setCompanyversion(cadDocDO.getCompanyversion());
            result.setMasteredoid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getMasteredoid()));
            result.setDocIterationid(cadDocDO.getIterationid());
            result.setLockstateinfo(cadDocDO.getLockstateinfo());
            result.setSecuritylabels(cadDocDO.getSecuritylabels());
            result.setSourceiterationoid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getSourceiterationoid()));
            String[] docnumberStr = docnumber.split(",");
            if (docnumberStr.length > 2) {
                result.setDocNumber(QueryDownloadDocUtil.parseStringByStr(docnumberStr[0]));
                result.setDocName(QueryDownloadDocUtil.parseStringByStr(docnumberStr[1]));
            } else {
                result.setDocNumber(cadDocDO.getDocnumber());
                result.setDocName(cadDocDO.getName());
            }

            return result;
        }
    }

    public List<DownloadDocVO> batchGetCadDocChildList(List<DownloadDocVO> cadDocList, boolean isAsStored, UserDO userDO) {
        log.info(">>>>>batchGetCadDocChildList");
        List<DownloadDocVO> result = new ArrayList();
        if (CollUtil.isEmpty(cadDocList)) {
            return result;
        } else {
            Map<String, Object> requestContext = RequestContext.currentContext();
            Boolean isIgnore = TyAccountContext.getIgnoreAuth();
            TyAccountContext.setIgnoreAuth(true);
            result.addAll(cadDocList);
            List<BigInteger> hasSelectOidList = (List)cadDocList.stream().filter((obj) -> {
                return StringUtils.isNoneBlank(new CharSequence[]{obj.getOid()});
            }).map((obj) -> {
                return CommonUtil.stringToBigInteger(obj.getOid());
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(hasSelectOidList)) {
                log.info(">>>>cadDocList" + StringUtils.join(hasSelectOidList, ";"));
            }

            List<BigInteger> treeChildList = new CopyOnWriteArrayList();
            List<DownloadDocVO> afterDocList = new CopyOnWriteArrayList();
            cadDocList.parallelStream().forEach((downloadDocVO) -> {
                RequestContext.attachContext(requestContext);
                log.info(">>downloadcadDocVO number:" + downloadDocVO.getDocNumber());
                BigInteger downloadOid = new BigInteger(downloadDocVO.getOid());
                if (!treeChildList.contains(downloadOid)) {
                    CadStructQueryDTO queryDTO = new CadStructQueryDTO();
                    queryDTO.setLevel(0);
                    List<BigInteger> oidList = new ArrayList();
                    oidList.add(CommonUtil.stringToBigInteger(downloadDocVO.getOid()));
                    queryDTO.setIdList(oidList);
                    if (isAsStored) {
                        queryDTO.setQueryType("asstored");
                    }

                    log.info(">>>>>cadDocTreeQuery:" + StringUtils.join(new Object[]{queryDTO, ";"}));
                    List<CadStructBaseVO> childDocTreeList = this.typlmCadStructQueryService.queryCadStructByThreeBusiness(queryDTO, CadStructScopeEnum.CAD_STRUCT_SCOPE_TREE);
                    if (CollUtil.isNotEmpty(childDocTreeList)) {
                        List<BigInteger> treeChildOidList = (List)childDocTreeList.stream().map(CadStructBaseVO::getChildId).collect(Collectors.toList());
                        if (CollUtil.isNotEmpty(treeChildOidList)) {
                            log.info(">>>>>treeChildOidList:" + StringUtils.join(treeChildOidList, ";"));
                            treeChildList.addAll(treeChildOidList);
                        }

                        childDocTreeList = (List)childDocTreeList.stream().filter((obj) -> {
                            return !hasSelectOidList.contains(obj.getChildId());
                        }).collect(Collectors.toList());
                    }

                    this.parseDownloadDocVOByCadDocTreeVO(childDocTreeList, afterDocList, downloadDocVO);
                }
            });
            if (CollUtil.isNotEmpty(afterDocList)) {
                result.addAll(afterDocList);
            }

            TyAccountContext.setIgnoreAuth(isIgnore);
            return result;
        }
    }

    private void parseDownloadDocVOByCadDocTreeVO(List<CadStructBaseVO> childDocTreeList, List<DownloadDocVO> result, DownloadDocVO parentDownloadDocVO) {
        log.info(">>>>>parseDownloadDocVOByCadDocTreeVO  start");
        if (!CollUtil.isEmpty(childDocTreeList)) {
            List<BigInteger> cadDocOidList = (List)childDocTreeList.stream().map(CadStructBaseVO::getChildId).distinct().collect(Collectors.toList());
            if (CollUtil.isNotEmpty(cadDocOidList)) {
                log.info(">>>>cadDocOidList:" + StringUtils.join(cadDocOidList, ";"));
            }

            String parentDownloadRight = parentDownloadDocVO.getHasDownloadRight();
            String parentManageRight = parentDownloadDocVO.getHasManageRight();
            String parentJudgeRight = parentDownloadDocVO.getIsJudgeRight();
            log.info(">>parentDownloadRight:" + parentDownloadRight + ">>>parentManageRight" + parentManageRight + ">>parentJudgeRight:" + parentJudgeRight);
            List<DownloadDocVO> childCadDocList = (List)childDocTreeList.stream().map(this::parseDownloadDocVOByCadDocTreeVO).collect(Collectors.toList());
            List<DownloadDocVO> child = new ArrayList();
            List<CADDocDO> numberList;
            if (CollUtil.isNotEmpty(cadDocOidList)) {
                numberList = this.typlmCADDocService.queryCADDocByOIDs(cadDocOidList);
                Map<String, CADDocDO> cadDocDOMap = (Map)numberList.stream().collect(Collectors.toMap((obj) -> {
                    return String.valueOf(obj.getOid());
                }, Function.identity(), (k1, k2) -> {
                    return k1;
                }));
                List<DownloadDocVO> dealList = new ArrayList();
                Iterator var13 = childCadDocList.iterator();

                label34:
                while(true) {
                    DownloadDocVO downloadDocVO1;
                    CADDocDO cadDocDO;
                    do {
                        if (!var13.hasNext()) {
                            break label34;
                        }

                        downloadDocVO1 = (DownloadDocVO)var13.next();
                        String docOid = downloadDocVO1.getOid();
                        cadDocDO = (CADDocDO)cadDocDOMap.get(docOid);
                    } while(ObjectUtils.isEmpty(cadDocDO) && dealList.contains(downloadDocVO1));

                    dealList.add(downloadDocVO1);
                    this.checkRefObjPermission(downloadDocVO1, parentDownloadRight, parentManageRight, parentJudgeRight);
                    downloadDocVO1.setFamilytablestatus(cadDocDO.getFamilytablestatus());
                    downloadDocVO1.setMasteredoid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getMasteredoid()));
                    downloadDocVO1.setTypeOid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getTypeoid()));
                    downloadDocVO1.setDocSubfolderOid(QueryDownloadDocUtil.parseStringByBigInteger(cadDocDO.getSubfolderoid()));
                    log.info(">>>downloadDocVO1:" + JSON.toJSONString(downloadDocVO1));
                    downloadDocVO1.setCompanyversion(cadDocDO.getCompanyversion());
                    downloadDocVO1.setFamilytablestatus(cadDocDO.getFamilytablestatus());
                    downloadDocVO1.setSecuritylabels(cadDocDO.getSecuritylabels());
                    child.add(downloadDocVO1);
                }
            }

            if (CollUtil.isNotEmpty(child)) {
                numberList = (List)child.stream().map(DownloadDocVO::getDocNumber).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(numberList)) {
                    log.info(">>>>numberList:" + StringUtils.join(numberList, ","));
                }

                result.addAll(child);
            }

            log.info(">>>>>parseDownloadDocVOByCadDocTreeVO  end");
        }
    }

    public DownloadDocVO parseDownloadDocVOByCadDocTreeVO(CadStructBaseVO cadStructBaseVO) {
        CadDocTreeVO cadDocTreeVO = (CadDocTreeVO)cadStructBaseVO;
        DownloadDocVO downloadDocVO = new DownloadDocVO();
        downloadDocVO.setOid(String.valueOf(cadDocTreeVO.getChildId()));
        downloadDocVO.setOtype("ty.inteplm.cad.CTyCADDoc");
        downloadDocVO.setDocName(cadDocTreeVO.getName());
        downloadDocVO.setDocNumber(cadDocTreeVO.getNumber());
        downloadDocVO.setDocType(cadDocTreeVO.getAuthoringApplication());
        downloadDocVO.setDocIterationid(cadDocTreeVO.getIteration());
        downloadDocVO.setDocVersion(cadDocTreeVO.getVersion());
        downloadDocVO.setIsJudgeRight("0");
        downloadDocVO.setHasDownloadRight("1");
        downloadDocVO.setHasManageRight("1");
        return downloadDocVO;
    }

    private void checkRefObjPermission(DownloadDocVO currentDownloadDocVO, String parentDownloadRight, String parentManageRight, String isJudgeRight) {
        currentDownloadDocVO.setIsJudgeRight(isJudgeRight);
        currentDownloadDocVO.setHasManageRight(parentManageRight);
        currentDownloadDocVO.setHasDownloadRight(parentDownloadRight);
    }

    public List<DownloadFileVO> parseDownloadFileByContentDataFileVo(DownloadDocVO downloadDocVO, List<FileDataVO> contentList) {
        List<DownloadFileVO> result = new ArrayList();
        if (ObjectUtils.isEmpty(downloadDocVO)) {
            return (List)result;
        } else {
            List<DownloadFileVO> oldDownloadList = downloadDocVO.getDownloadFileList();
            if (CollUtil.isNotEmpty(oldDownloadList)) {
                result = oldDownloadList;
            }

            String isJudgeRight = downloadDocVO.getIsJudgeRight();
            String downloadRight = downloadDocVO.getHasDownloadRight();
            String manageRight = downloadDocVO.getHasManageRight();
            BigDecimal oldFileSize = FileUtil.formatFileSize(downloadDocVO.getDocSize());
            List<String> selectPreferenceValueList = Arrays.asList("OutputRightSeparate");
            Map<String, Object> preferenceCollectRulesMap = this.typlmPreferenceValueUtil.readPreferenceValueByKeywords(selectPreferenceValueList);
            Boolean outputRightSeparate = QueryDownloadDocUtil.getBooleanValueByObject(preferenceCollectRulesMap.get("OutputRightSeparate"));
            log.info(">>>>>outputRightSeparate:" + outputRightSeparate);
            Iterator var12 = contentList.iterator();

            while(true) {
                FileDataVO fileDataVO;
                DownloadFileVO downloadFileVO;
                while(true) {
                    if (!var12.hasNext()) {
                        downloadDocVO.setDocSize(oldFileSize.toPlainString());
                        return (List)result;
                    }

                    fileDataVO = (FileDataVO)var12.next();
                    String roleKey = fileDataVO.getRolekey();
                    downloadFileVO = new DownloadFileVO();
                    downloadFileVO.setIspermission(downloadRight);
                    if (!"PRIMARY".equals(roleKey) && !"SECONDARY".equals(roleKey)) {
                        if (!"INTEVUE".equalsIgnoreCase(roleKey)) {
                            continue;
                        }

                        downloadFileVO.setFileType("2");
                        break;
                    }

                    downloadFileVO.setFileType("1");
                    if (outputRightSeparate && !"2".equals(isJudgeRight)) {
                        downloadFileVO.setIspermission("0");
                        if ("1".equals(downloadRight) && "1".equals(manageRight)) {
                            downloadFileVO.setIspermission("1");
                        }
                    }
                    break;
                }

                downloadFileVO.setFileId(fileDataVO.getFileID());
                downloadFileVO.setFileName(fileDataVO.getFileName());
                String fileSize = fileDataVO.getFileSize();
                downloadFileVO.setFileSize(fileSize);
                oldFileSize = oldFileSize.add(new BigDecimal(fileSize));
                ((List)result).add(downloadFileVO);
            }
        }
    }

    private CadDocTreeQuery getCadDocTreeQuery(BigInteger cadDocOid, String userOid, boolean isAsStored) {
        List<BigInteger> oidList = new ArrayList();
        oidList.add(cadDocOid);
        CadDocTreeQuery cadDocTreeQuery = new CadDocTreeQuery();
        if (isAsStored) {
            cadDocTreeQuery.setIsAsStored("Y");
        }

        cadDocTreeQuery.setId(cadDocOid.toString());
        cadDocTreeQuery.setChildLevel("ALL");
        cadDocTreeQuery.setUserId(userOid);
        return cadDocTreeQuery;
    }

    public void parseFileDownInfoByDownloadOptionVO(DownloadOptionVO downloadOptionVO, FileDownCenterDO fileDownCenterDO, List<FileDownQueueDO> fileDownQueueDOList, UserDO userDO, String isFolderPath) {
        TyplmDocumentService typlmDocumentService = SpringUtil.getBean(TyplmDocumentService.class);
        List<DownloadDocVO> downloadDocVOS=downloadOptionVO.getDownloadDocList();
        if(downloadDocVOS!=null){
            for(int i=0;i<downloadDocVOS.size();i++){
                List<DownloadFileVO> downloadFileVOList=downloadDocVOS.get(i).getDownloadFileList();
                if(downloadFileVOList!=null && downloadFileVOList.size()>0){
                    String drawingNumber="";
                    String drawingView="";

                    if("ty.inteplm.cad.CTyCADDoc".equals(downloadDocVOS.get(i).getOtype())){
                        BigInteger oid = new BigInteger(downloadDocVOS.get(i).getOid());
                        String oType = downloadDocVOS.get(i).getOtype();
                        DocDetails docDetails = typlmDocumentService.queryDocDetails(oid, oType);
                        Map<String, List<String>> attr=docDetails.getIbaAttributeMap();
                        if(attr.containsKey("代号")){
                            List<String> list=attr.get("代号");
                            drawingNumber=list.get(0);
                        }
//                        if(attr.containsKey("版本")){
//                            List<String> list=attr.get("版本");
//                            drawingView=list.get(0);
//                        }
//                        else
                            if(attr.containsKey("图纸版本")){
                            List<String> list=attr.get("图纸版本");
                            drawingView=list.get(0);
                        }
                    }
                    else if("ty.inteplm.doc.CTyDoc".equals(downloadDocVOS.get(i).getOtype())){
                        BigInteger oid = new BigInteger(downloadDocVOS.get(i).getOid());
                        String oType = downloadDocVOS.get(i).getOtype();
                        DocDetails docDetails = typlmDocumentService.queryDocDetails(oid, oType);
                        Map<String, List<String>> attr=docDetails.getIbaAttributeMap();
                        if(attr.containsKey("代号")){
                            List<String> list=attr.get("代号");
                            drawingNumber=list.get(0);
                        }
//                        if(attr.containsKey("版本")){
//                            List<String> list=attr.get("版本");
//                            drawingView=list.get(0);
//                        }
//                        else
                            if(attr.containsKey("图纸版本")){
                            List<String> list=attr.get("图纸版本");
                            drawingView=list.get(0);
                        }
                    }

                    for(int a=0;a<downloadFileVOList.size();a++){
                        String fileName=downloadFileVOList.get(a).getFileName();
                        String newName=fileName;
                        String changName=fileName.substring(fileName.lastIndexOf("."));
                        newName=String.format("%s-%s-%s_%s%s",downloadDocVOS.get(i).getDocNumber(),downloadDocVOS.get(i).getName(),drawingNumber,drawingView,changName);
                        downloadFileVOList.get(a).setFileName(newName);
                    }
                }
            }
        }

        BigInteger userOid = userDO.getOid();
        String userName = userDO.getFullname();
        BigInteger centerOid = this.snowflakeIdComponent.getInstance().nextId();
        fileDownCenterDO.setOid(centerOid);
        fileDownCenterDO.setDealtime(new Date());
        fileDownCenterDO.setState(FileDownConstant.FILEDOWN_STATE_PACKING.toString());
        fileDownCenterDO.setFilename(downloadOptionVO.getZipFileName());
        fileDownCenterDO.setFileownername(userName);
        fileDownCenterDO.setFileowneroid(userOid);
        fileDownCenterDO.setCreatetime(new Date());
        fileDownCenterDO.setIsfolderpath(Integer.valueOf(isFolderPath));
        List<DownloadDocVO> downloadOptionVO1 = downloadOptionVO.getDownloadDocList();
        boolean hasPermissionList = false;
        int total = 0;
        Iterator var12 = downloadOptionVO1.iterator();

        while(true) {
            DownloadDocVO downloadDocVO;
            List downloadFileVOList;
            do {
                if (!var12.hasNext()) {
                    fileDownCenterDO.setFiletotal(total);
                    if (!hasPermissionList && CollUtil.isNotEmpty(fileDownQueueDOList) && downloadOptionVO.getHasPermissionMeassage()) {
                        throw FileErrorCodeEnum.ALL_FILE_NOT_PERMISSION.getException(new Object[0]);
                    }

                    return;
                }

                downloadDocVO = (DownloadDocVO)var12.next();
                downloadFileVOList = downloadDocVO.getDownloadFileList();
            } while(!CollUtil.isNotEmpty(downloadFileVOList));

            total += downloadFileVOList.size();

            FileDownQueueDO fileDownQueueDO;
            for(Iterator var15 = downloadFileVOList.iterator(); var15.hasNext(); fileDownQueueDOList.add(fileDownQueueDO)) {
                DownloadFileVO downloadFileVO = (DownloadFileVO)var15.next();
                fileDownQueueDO = this.parseDownloadFileVO(downloadOptionVO, downloadDocVO, downloadFileVO, userOid, userName, centerOid);
                if ("1".equals(downloadFileVO.getIspermission())) {
                    hasPermissionList = true;
                }
            }
        }
    }

    private FileDownQueueDO parseDownloadFileVO(DownloadOptionVO downloadOptionVO, DownloadDocVO downloadDocVO, DownloadFileVO downloadFileVO, BigInteger userOid, String userName, BigInteger centerOid) {
        FileDownQueueDO fileDownQueueDO = new FileDownQueueDO();
        BigInteger queueOid = this.snowflakeIdComponent.getInstance().nextId();
        fileDownQueueDO.setTaskoid(centerOid);
        fileDownQueueDO.setCreatetime(new Date());
        fileDownQueueDO.setEndtime(new Date());
        fileDownQueueDO.setHasdownloadright(Integer.valueOf(downloadDocVO.getHasDownloadRight()));
        fileDownQueueDO.setIsjudgeright(Integer.valueOf(downloadDocVO.getIsJudgeRight()));
        fileDownQueueDO.setIspermission(Integer.valueOf(downloadFileVO.getIspermission()));
        fileDownQueueDO.setSignature(Integer.valueOf(downloadOptionVO.getIsSign()));
        fileDownQueueDO.setWaterroute(downloadDocVO.getWaterRoute());
        fileDownQueueDO.setWatercontent(downloadDocVO.getWaterContent());
        fileDownQueueDO.setIswater(Integer.valueOf(downloadOptionVO.getIsWater()));
        fileDownQueueDO.setFileoid(CommonUtil.stringToBigInteger(downloadFileVO.getFileId()));
        fileDownQueueDO.setFilename(downloadFileVO.getFileName());
        fileDownQueueDO.setFileowneroid(userOid);
        fileDownQueueDO.setFileownername(userName);
        fileDownQueueDO.setFiletype(downloadFileVO.getFileType());
        fileDownQueueDO.setState(FileDownConstant.FILEDOWN_STATE_PACKING.toString());
        fileDownQueueDO.setFilesize(downloadFileVO.getFileSize());
        fileDownQueueDO.setDocsize(downloadDocVO.getDocSize());
        fileDownQueueDO.setDocfoldername(downloadDocVO.getFolderName());
        fileDownQueueDO.setOid(queueOid);
        fileDownQueueDO.setDocoid(CommonUtil.stringToBigInteger(downloadDocVO.getOid()));
        fileDownQueueDO.setDocname(downloadDocVO.getDocName());
        fileDownQueueDO.setDocotype(downloadDocVO.getDocType());
        fileDownQueueDO.setDocnumber(downloadDocVO.getDocNumber());
        fileDownQueueDO.setDocversion(downloadDocVO.getDocVersion());
        fileDownQueueDO.setDoccombinname(downloadDocVO.getCombinName());
        return fileDownQueueDO;
    }

    public void getAllChildFolderByParent(BigInteger subFolderOid, String subFolderOtype, List<IdentifierEntity> subfolderList) {
        List<IdentifierEntity> childList = this.typlmSubFolderLinkService.queryFolderListByParent(subFolderOid, subFolderOtype);
        if (CollUtil.isNotEmpty(childList)) {
            subfolderList.addAll(childList);
        }

    }

    private void dealWaterContent(String waterContent, String waterRoute, String waterType, DownloadDocVO downloadDocVO, UserDO userDO) {
        log.info(">>>dealWaterContent start");
        if (!StringUtils.isBlank(downloadDocVO.getOid()) && !StringUtils.isBlank(downloadDocVO.getOtype())) {
            if (StringUtils.isNoneBlank(new CharSequence[]{waterRoute})) {
                downloadDocVO.setWaterRoute(waterRoute);
            }

            String newWaterContent = "";
            if ("当前用户账号+名字+禁止外泄".equals(waterType)) {
                if (ObjectUtils.isEmpty(userDO)) {
                    throw SystemErrorCodeEnum.USER_SESSION_INVALID.getException(new Object[0]);
                }

                newWaterContent = userDO.getName() + "-" + userDO.getFullname() + "-" + FileErrorCodeEnum.FORBIDDEN_LEAKAGE.getLocaleMessage();
            } else {
                newWaterContent = QueryDownloadDocUtil.parseStringByStr(waterContent);
            }

            log.info(">>>newWaterContent" + newWaterContent);
            downloadDocVO.setWaterContent(newWaterContent);
        }
    }

    public List<DownloadDocVO> paresAllDoc(List<DownloadDocVO> docList, List<DownloadDocVO> cadDocList, DownloadOptionVO result, UserDO userDO) {
        List<DownloadDocVO> allList = new ArrayList();
        if (CollUtil.isNotEmpty(docList)) {
            allList.addAll(docList);
        }

        if (CollUtil.isNotEmpty(cadDocList)) {
            this.getCADReference(cadDocList);
            allList.addAll(cadDocList);
        }

        if (CollUtil.isNotEmpty(allList)) {
            String waterContent = result.getWaterContent();
            String waterRoute = result.getWaterRoute();
            String waterType = result.getWaterType();
            allList.forEach((obj) -> {
                this.dealWaterContent(waterContent, waterRoute, waterType, obj, userDO);
            });
        }

        return allList;
    }

    private void getCADReference(List<DownloadDocVO> cadDocList) {
        log.info(">>>>>getCADReference  start");
        if (!CollUtil.isEmpty(cadDocList)) {
            List<DownloadDocVO> allRefCadList = new ArrayList();
            List<BigInteger> allCadOidList = (List)cadDocList.stream().filter((obj) -> {
                return StringUtils.isNoneBlank(new CharSequence[]{obj.getOid()});
            }).map((obj) -> {
                return CommonUtil.stringToBigInteger(obj.getOid());
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(allCadOidList)) {
                Map<BigInteger, List<BigInteger>> cadRedDocMap = this.buildCadReferenceAndReferenced(allCadOidList);
                cadDocList.forEach((downloadDocVO) -> {
                    List<DownloadDocVO> refDocList = new ArrayList();
                    String otype = downloadDocVO.getOtype();
                    String parentDownloadRight = downloadDocVO.getHasDownloadRight();
                    String parentManageRight = downloadDocVO.getHasManageRight();
                    String parentIsJudgeRight = downloadDocVO.getIsJudgeRight();
                    if ("ty.inteplm.cad.CTyCADDoc".equals(otype) || StringUtils.isNotBlank(downloadDocVO.getOid())) {
                        BigInteger oid = new BigInteger(downloadDocVO.getOid());
                        log.info(">>>>cadoid:" + oid);
                        List<BigInteger> cadRelDocList = (List)cadRedDocMap.get(oid);
                        this.dealRefCadDocList(cadRelDocList, allCadOidList, parentDownloadRight, parentManageRight, parentIsJudgeRight, refDocList);
                        if (CollUtil.isNotEmpty(refDocList)) {
                            allRefCadList.addAll(refDocList);
                        }
                    }

                });
            }

            if (CollUtil.isNotEmpty(allRefCadList)) {
                cadDocList.addAll(allRefCadList);
            }

            log.info(">>>>getCADReference  end");
        }
    }

    private Map<BigInteger, List<BigInteger>> buildCadReferenceAndReferenced(List<BigInteger> cadIdList) {
        Map<BigInteger, List<BigInteger>> result = new HashMap();
        List<CadReferenceDTO> cadReferencedList = this.typlmCADReferenceLinkService.batchQueryCadReferencedLinkByStored(cadIdList, CommonUtil.getUserOid());
        if (CollUtil.isNotEmpty(cadReferencedList)) {
            result = (Map)cadReferencedList.stream().collect(Collectors.groupingBy(CadReferenceDTO::getCadId, Collectors.mapping(CadReferenceDTO::getReferencedCadId, Collectors.toList())));
        }

        List<CadReferenceDTO> cadReferenceList = this.typlmCADReferenceLinkService.batchQueryCadReferenceLinkByStored(cadIdList, CommonUtil.getUserOid());
        if (CollUtil.isNotEmpty(cadReferenceList)) {
            Map<BigInteger, List<BigInteger>> finalResult = result;
            cadReferenceList.forEach((obj) -> {
                BigInteger cadOid = obj.getCadId();
                List<BigInteger> redDocOidList = new ArrayList();
                if (finalResult.containsKey(cadOid)) {
                    redDocOidList = (List) finalResult.get(cadOid);
                }

                ((List)redDocOidList).add(obj.getReferenceCadId());
                finalResult.put(cadOid, redDocOidList);
            });
            return (Map)result;
        } else {
            return (Map)result;
        }
    }

    private void dealRefCadDocList(List<BigInteger> cadRelDocList, List<BigInteger> allCadOidList, String parentDownloadRight, String parentManageRight, String parentIsJudgeRight, List<DownloadDocVO> refDocList) {
        if (!CollUtil.isEmpty(cadRelDocList)) {
            List<DownloadDocVO> list = new ArrayList();
            List<BigInteger> redOidList = (List)cadRelDocList.stream().filter((obj) -> {
                return !allCadOidList.contains(obj);
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(redOidList)) {
                allCadOidList.addAll(redOidList);
                List<CADDocDO> cadDocDOList = this.typlmCADDocService.queryCADDocByOIDs(redOidList);
                if (CollUtil.isNotEmpty(cadDocDOList)) {
                    List<String> nameList = (List)cadDocDOList.stream().map(CADDocDO::getDocnumber).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(nameList)) {
                        log.info(">>>All Reference dco:" + StringUtils.join(nameList, ","));
                    }

                    list = (List)cadDocDOList.stream().map((obj) -> {
                        return this.parseDownloadDocVOByRefCADDocDO(obj, parentDownloadRight, parentManageRight, parentIsJudgeRight);
                    }).filter(ObjectUtils::isNotEmpty).collect(Collectors.toList());
                }
            }

            if (CollUtil.isNotEmpty((Collection)list)) {
                refDocList.addAll((Collection)list);
            }

        }
    }

    public List<DownloadDocVO> parseDownloadDocVOByPartRefDocVOList(List<PartRefDocVO> partRefDocVOList, UserDO userDO, List<BigInteger> selectDocTypeOidList) {
        List<DownloadDocVO> result = new ArrayList();
        if (CollUtil.isEmpty(partRefDocVOList)) {
            return result;
        } else {
            List<String> docOidList = (List)partRefDocVOList.stream().map(PartRefDocVO::getOid).collect(Collectors.toList());
            List<DocDO> docDOList = this.typlmDocQueryService.getDocDOListByOids(docOidList);
            if (CollUtil.isNotEmpty(docDOList)) {
                docDOList = (List)docDOList.stream().distinct().filter((obj) -> {
                    return selectDocTypeOidList.contains(obj.getTypeoid());
                }).collect(Collectors.toList());
            }

            if (CollUtil.isNotEmpty(docDOList)) {
                Map<String, DocDO> docMap = (Map)docDOList.stream().collect(Collectors.toMap((obj) -> {
                    return QueryDownloadDocUtil.parseStringByBigInteger(obj.getOid());
                }, Function.identity(), (k1, k2) -> {
                    return k1;
                }));
                List<DownloadDocVO> docVOList = (List)partRefDocVOList.stream().map((obj) -> {
                    return this.parseDownloadDocVOByDocDOAndPartRefDocVO(obj, docMap);
                }).filter(ObjectUtils::isNotEmpty).collect(Collectors.toList());
                List<CTyEntityBaseDO> cTyEntityBaseDOList = new ArrayList(docDOList);
                List<DownloadDocVO> finallyDocVOList = this.checkPermissions(docVOList, cTyEntityBaseDOList, userDO);
                if (CollUtil.isNotEmpty(finallyDocVOList)) {
                    result.addAll(finallyDocVOList);
                }
            }

            return result;
        }
    }

    public List<DownloadDocVO> parseDownloadDocByPartRefDocVOList(List<DocDO> partRefDocVOList, UserDO userDO, List<BigInteger> selectDocTypeOidList) {
        List<DownloadDocVO> result = new ArrayList();
        if (CollUtil.isEmpty(partRefDocVOList)) {
            return result;
        } else {
            if (CollUtil.isNotEmpty(partRefDocVOList)) {
                partRefDocVOList = (List)partRefDocVOList.stream().distinct().filter((obj) -> {
                    return selectDocTypeOidList.contains(obj.getTypeoid());
                }).collect(Collectors.toList());
            }

            if (CollUtil.isNotEmpty(partRefDocVOList)) {
                List<DownloadDocVO> docVOList = (List)partRefDocVOList.stream().map(this::parseDownloadDocVOByRefDocDO).filter(ObjectUtils::isNotEmpty).collect(Collectors.toList());
                List<CTyEntityBaseDO> cTyEntityBaseDOList = new ArrayList(partRefDocVOList);
                List<DownloadDocVO> finallyDocVOList = this.checkPermissions(docVOList, cTyEntityBaseDOList, userDO);
                if (CollUtil.isNotEmpty(finallyDocVOList)) {
                    result.addAll(finallyDocVOList);
                }
            }

            return result;
        }
    }

    private DownloadDocVO parseDownloadDocVOByDocDOAndPartRefDocVO(PartRefDocVO partRefDocVO, Map<String, DocDO> docMap) {
        DownloadDocVO downloadDocVO = new DownloadDocVO();
        if (!ObjectUtils.isEmpty(partRefDocVO) && !CollUtil.isEmpty(docMap)) {
            String docOid = partRefDocVO.getOid();
            DocDO docDO = (DocDO)docMap.get(docOid);
            if (ObjectUtils.isEmpty(docDO)) {
                return null;
            } else {
                downloadDocVO.setDocNumber(partRefDocVO.getDocNumber());
                downloadDocVO.setDocName(partRefDocVO.getName());
                downloadDocVO.setOid(partRefDocVO.getOid());
                downloadDocVO.setTypeOid(QueryDownloadDocUtil.parseStringByBigInteger(docDO.getTypeoid()));
                downloadDocVO.setDocSubfolderOid(QueryDownloadDocUtil.parseStringByBigInteger(docDO.getSubfolderoid()));
                downloadDocVO.setOtype(docDO.getOtype());
                downloadDocVO.setDocVersion(docDO.getVersion());
                downloadDocVO.setDocIterationid(docDO.getIterationid());
                downloadDocVO.setLockstateinfo(docDO.getLockstateinfo());
                downloadDocVO.setSourceiterationoid(QueryDownloadDocUtil.parseStringByBigInteger(docDO.getSourceiterationoid()));
                downloadDocVO.setSecuritylabels(docDO.getSecuritylabels());
                QueryDownloadDocUtil.parseDefaultDownloadDocVOIsJudgeRight(downloadDocVO);
                return downloadDocVO;
            }
        } else {
            return null;
        }
    }

    private DownloadDocVO parseDownloadDocVOByRefDocDO(DocDO doc) {
        DownloadDocVO downloadDocVO = new DownloadDocVO();
        if (ObjectUtils.isEmpty(doc)) {
            return null;
        } else {
            downloadDocVO.setDocNumber(doc.getDocnumber());
            downloadDocVO.setDocName(doc.getName());
            downloadDocVO.setOid(String.valueOf(doc.getOid()));
            downloadDocVO.setTypeOid(QueryDownloadDocUtil.parseStringByBigInteger(doc.getTypeoid()));
            downloadDocVO.setDocSubfolderOid(QueryDownloadDocUtil.parseStringByBigInteger(doc.getSubfolderoid()));
            downloadDocVO.setOtype(doc.getOtype());
            downloadDocVO.setDocVersion(doc.getVersion());
            downloadDocVO.setDocIterationid(doc.getIterationid());
            downloadDocVO.setLockstateinfo(doc.getLockstateinfo());
            downloadDocVO.setSourceiterationoid(QueryDownloadDocUtil.parseStringByBigInteger(doc.getSourceiterationoid()));
            downloadDocVO.setSecuritylabels(doc.getSecuritylabels());
            QueryDownloadDocUtil.parseDefaultDownloadDocVOIsJudgeRight(downloadDocVO);
            return downloadDocVO;
        }
    }

    private List<CTyEntityBaseDO> queryCTyEntityBaseDOByDownloadDocVOList(List<DownloadDocVO> docList) {
        List<CTyEntityBaseDO> cTyEntityBaseDOList = new ArrayList();
        if (CollUtil.isEmpty(docList)) {
            return cTyEntityBaseDOList;
        } else {
            String otype = ((DownloadDocVO)docList.get(0)).getOtype();
            List docOidList;
            List docDOList;
            if ("ty.inteplm.cad.CTyCADDoc".equals(otype)) {
                docOidList = this.queryOidListByCondition(docList, "ty.inteplm.cad.CTyCADDoc");
                if (CollUtil.isEmpty(docOidList)) {
                    return cTyEntityBaseDOList;
                }

                docDOList = this.typlmCADDocService.queryCADDocByOIDs(docOidList);
                if (CollUtil.isNotEmpty(docDOList)) {
                    cTyEntityBaseDOList.addAll(docDOList);
                }
            }

            if ("ty.inteplm.doc.CTyDoc".equals(otype)) {
                docOidList = this.queryOidListByCondition(docList, "ty.inteplm.doc.CTyDoc");
                if (CollUtil.isEmpty(docOidList)) {
                    return cTyEntityBaseDOList;
                }

                docDOList = this.typlmDocQueryService.queryDocByIds(docOidList);
                if (CollUtil.isNotEmpty(docDOList)) {
                    cTyEntityBaseDOList.addAll(docDOList);
                }
            }

            return cTyEntityBaseDOList;
        }
    }

    private List<BigInteger> queryOidListByCondition(List<DownloadDocVO> docList, String otype) {
        List<BigInteger> oidList = new ArrayList();
        if (!CollUtil.isEmpty(docList) && !StringUtils.isBlank(otype)) {
            docList = (List)docList.stream().filter((obj) -> {
                return otype.equals(obj.getOtype()) && !"0".equals(String.valueOf(obj.getIsJudgeRight()));
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(docList)) {
                oidList = (List)docList.stream().map((obj) -> {
                    return CommonUtil.stringToBigInteger(obj.getOid());
                }).collect(Collectors.toList());
            }

            return (List)oidList;
        } else {
            return (List)oidList;
        }
    }

    public List<DownloadDocVO> checkPermissions(List<DownloadDocVO> docList, List<CTyEntityBaseDO> cTyEntityBaseDOList, UserDO userDO) {
        log.info(">>>>>checkPermissions start");
        if (CollUtil.isEmpty(docList)) {
            return docList;
        } else {
            List<DownloadDocVO> result = new ArrayList(docList);
            if (CollUtil.isEmpty(cTyEntityBaseDOList)) {
                log.info(">>>>queryCTyEntityBaseDOByDownloadDocVOList satrt");
                cTyEntityBaseDOList = this.queryCTyEntityBaseDOByDownloadDocVOList(docList);
            }

            if (CollUtil.isEmpty(cTyEntityBaseDOList)) {
                return result;
            } else {
                log.info(">>>>cTyEntityBaseDOList start" + cTyEntityBaseDOList.size());
                List<String> downPermission = new ArrayList();
                List<String> administrativePermission = new ArrayList();

                for(int i = 0; i < cTyEntityBaseDOList.size(); ++i) {
                    downPermission.add("下载");
                    administrativePermission.add("管理");
                }

                List<TyBaseObject> objects = new ArrayList(cTyEntityBaseDOList);
                List<AccessBatchDTO> downAccessBatchList = this.typlmAccessJudgeService.checkAccessByObjectId(objects, userDO, downPermission);
                Map<String, AccessBatchDTO> downMap = new HashMap();
                if (CollUtil.isNotEmpty(downAccessBatchList)) {
                    downMap = (Map)downAccessBatchList.stream().collect(Collectors.toMap(AccessBatchDTO::getObjectoid, Function.identity(), (k1, k2) -> {
                        return k1;
                    }));
                }

                Map<String, AccessBatchDTO> administrativeMap = new HashMap();
                List<String> selectPreferenceValueList = Collections.singletonList("OutputRightSeparate");
                Map<String, Object> preferenceCollectRulesMap = this.typlmPreferenceValueUtil.readPreferenceValueByKeywords(selectPreferenceValueList);
                boolean outputRightSeparate = QueryDownloadDocUtil.getBooleanValueByObject(preferenceCollectRulesMap.get("OutputRightSeparate"));
                log.info(">>>outputRightSeparate:" + outputRightSeparate);
                if (outputRightSeparate) {
                    List<AccessBatchDTO> administrativeAccessBatchList = this.typlmAccessJudgeService.checkAccessByObjectId(objects, userDO, administrativePermission);
                    if (CollUtil.isNotEmpty(administrativeAccessBatchList)) {
                        log.info(">>>administrativeAccessBatchList:" + administrativeAccessBatchList.size());
                        administrativeMap = (Map)administrativeAccessBatchList.stream().collect(Collectors.toMap(AccessBatchDTO::getObjectoid, Function.identity(), (k1, k2) -> {
                            return k1;
                        }));
                    }
                }

                Map<String, AccessBatchDTO> finalAdministrativeMap = administrativeMap;
                Map<String, AccessBatchDTO> finalDownMap = downMap;
                List<DownloadDocVO> result1 = (List)docList.stream().map((obj) -> {
                    return this.dealDownloadDocVOAccess(obj, finalAdministrativeMap, finalDownMap, outputRightSeparate);
                }).collect(Collectors.toList());
                return result1;
            }
        }
    }

    private DownloadDocVO dealDownloadDocVOAccess(DownloadDocVO downloadDocVO, Map<String, AccessBatchDTO> administrativeMap, Map<String, AccessBatchDTO> downMap, boolean outputRightSeparate) {
        log.info(">>>>>dealDownloadDocVOAccess start");
        if (ObjectUtils.isEmpty(downloadDocVO)) {
            return downloadDocVO;
        } else {
            String oid = downloadDocVO.getOid();
            List<String> selectPreferenceValueList = Arrays.asList("OutputRightSeparate");
            AccessBatchDTO accessBatchDTO;
            String isDown;
            if (outputRightSeparate && administrativeMap.containsKey(oid)) {
                log.info(">>>administrative satrt");
                accessBatchDTO = (AccessBatchDTO)administrativeMap.get(oid);
                if (ObjectUtils.isNotEmpty(accessBatchDTO)) {
                    log.info(">>>administrative accessBatchDTO" + JSON.toJSONString(accessBatchDTO));
                    isDown = "Y".equals(accessBatchDTO.getIsAuthorized()) ? "1" : "0";
                    log.info(">>isAuthorized:" + isDown);
                    downloadDocVO.setHasManageRight(isDown);
                }
            }

            if (downMap.containsKey(oid)) {
                log.info(">>>downsatrt");
                accessBatchDTO = (AccessBatchDTO)downMap.get(oid);
                if (ObjectUtils.isNotEmpty(accessBatchDTO)) {
                    log.info(">>>downsatrt accessBatchDTO" + JSON.toJSONString(accessBatchDTO));
                    isDown = "Y".equals(accessBatchDTO.getIsAuthorized()) ? "1" : "0";
                    log.info(">>isDown:" + isDown);
                    downloadDocVO.setHasDownloadRight(isDown);
                }
            }

            return downloadDocVO;
        }
    }

    public void dealMessage(DownloadDocVO downloadDocVO, List<String> downList, List<String> manageList) {
        List<String> selectPreferenceValueList = Collections.singletonList("OutputRightSeparate");
        Map<String, Object> preferenceCollectRulesMap = this.typlmPreferenceValueUtil.readPreferenceValueByKeywords(selectPreferenceValueList);
        boolean outputRightSeparate = QueryDownloadDocUtil.getBooleanValueByObject(preferenceCollectRulesMap.get("OutputRightSeparate"));
        this.dealMessageByOutputRightSeparate(downloadDocVO, downList, manageList, outputRightSeparate);
    }

    public void dealMessageByOutputRightSeparate(DownloadDocVO downloadDocVO, List<String> downList, List<String> manageList, Boolean outputRightSeparate) {
        if (!ObjectUtils.isEmpty(downloadDocVO)) {
            String isJudgeRight = downloadDocVO.getIsJudgeRight();
            if (!"0".equals(isJudgeRight)) {
                String hasDownloadRight = downloadDocVO.getHasDownloadRight();
                String hasManageRight = downloadDocVO.getHasManageRight();
                String combinName = downloadDocVO.getIdentifierDisplayName();
                if (!"1".equals(hasDownloadRight)) {
                    downList.add(combinName);
                }

                int count = 0;
                if (CollUtil.isNotEmpty(downloadDocVO.getDownloadFileList())) {
                    List<DownloadFileVO> downloadFileList = downloadDocVO.getDownloadFileList();
                    List<DownloadFileVO> collect = (List)downloadFileList.stream().filter((downloadFileVO) -> {
                        return "1".equals(downloadFileVO.getFileType());
                    }).collect(Collectors.toList());
                    count = collect.size();
                }

                if (outputRightSeparate && "0".equals(hasManageRight) && count > 0) {
                    manageList.add(combinName);
                }

            }
        }
    }

    public List<BigInteger> getAllSubfolderOid(List<DownloadDocVO> downloadDocVOList, Map<BigInteger, BigInteger> oidAndSubfolderMap) {
        List<BigInteger> subfolderOid = new ArrayList();
        List<BigInteger> subfolderOidList = (List)downloadDocVOList.stream().filter((docVo) -> {
            return ObjectUtils.isNotEmpty(docVo.getDocSubfolderOid()) && !"wrk".equals(docVo.getLockstateinfo());
        }).distinct().map((docVo) -> {
            return CommonUtil.stringToBigInteger(docVo.getDocSubfolderOid());
        }).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(subfolderOidList)) {
            subfolderOid.addAll(subfolderOidList);
        }

        Map<String, List<String>> typeAnSourceOiddmap = (Map)downloadDocVOList.stream().filter((docVo) -> {
            return ObjectUtils.isNotEmpty(docVo.getSourceiterationoid()) && "wrk".equals(docVo.getLockstateinfo());
        }).distinct().collect(Collectors.groupingBy(DownloadDocVO::getOtype, Collectors.mapping(DownloadDocVO::getSourceiterationoid, Collectors.toList())));
        if (CollUtil.isEmpty(typeAnSourceOiddmap)) {
            return subfolderOid;
        } else {
            List<String> docOidList = (List)typeAnSourceOiddmap.get("ty.inteplm.doc.CTyDoc");
            List<DocDO> docDoList1;
            List docSubfolderOid;
            if (CollUtil.isNotEmpty(docOidList)) {
                docDoList1 = this.typlmDocQueryService.getDocDOListByOids(docOidList);
                if (CollUtil.isNotEmpty(docDoList1)) {
                    docSubfolderOid = (List)docDoList1.stream().distinct().map(DocDO::getSubfolderoid).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(docSubfolderOid)) {
                        subfolderOid.addAll(docSubfolderOid);
                        Map<BigInteger, BigInteger> docsubfolderMap = (Map)docDoList1.stream().collect(Collectors.toMap(DocDO::getOid, DocDO::getSubfolderoid, (k1, k2) -> {
                            return k1;
                        }));
                        oidAndSubfolderMap.putAll(docsubfolderMap);
                    }
                }
            }

            List<String> docDoList = (List)typeAnSourceOiddmap.get("ty.inteplm.cad.CTyCADDoc");
            if (CollUtil.isNotEmpty(docDoList)) {
                docSubfolderOid = (List)docDoList.stream().map(CommonUtil::stringToBigInteger).collect(Collectors.toList());
                List<CADDocDO> cadDocDOList = this.typlmCADDocService.queryCADDocByOIDs(docSubfolderOid);
                if (CollUtil.isNotEmpty(cadDocDOList)) {
                    List<BigInteger> cadDocSubfolderOid = (List)cadDocDOList.stream().distinct().map(CADDocDO::getSubfolderoid).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(cadDocSubfolderOid)) {
                        subfolderOid.addAll(cadDocSubfolderOid);
                        Map<BigInteger, BigInteger> caddocsubfolderMap = (Map)cadDocDOList.stream().collect(Collectors.toMap(CADDocDO::getOid, CADDocDO::getSubfolderoid, (k1, k2) -> {
                            return k1;
                        }));
                        oidAndSubfolderMap.putAll(caddocsubfolderMap);
                    }
                }
            }

            return subfolderOid;
        }
    }
}
